package com.libraryWeb.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.libraryWeb.dao.BookDao;
import com.libraryWeb.entity.Book;
import com.libraryWeb.util.DateUtil;



/**
 * Servlet implementation class BookServlet
 */
@WebServlet("/book")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	BookDao bookDao = new BookDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//统一设置编码处理
		request.setCharacterEncoding("utf-8");// post
		response.setContentType("text/html;charset=utf-8");
				
		//获取当前的动作
		String action = request.getParameter("action");
		if(action.equals("qryall")) {
			qryall(request,response);
		}else if(action.equals("add")) {
			add(request,response);
		}else if(action.equals("qryByid")) {
			qryById(request,response);
		}else if(action.equals("upd")) {
			updateById(request,response);
		}else if(action.equals("delByid")) {
			delById(request,response);
		}
		
	}
	
	protected void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1 获取相关图书的数据
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String counts = request.getParameter("counts");
		String author = request.getParameter("author");
		String crtTime = request.getParameter("crtTime");
		
		Book p = new Book();
		p.setId(Integer.parseInt(id));
		p.setName(name);
		p.setPrice(Double.parseDouble(price));
		p.setCounts(Integer.parseInt(counts));
		p.setAuthor(author);
		p.setCrtTime(DateUtil.strToUtilDate(crtTime));
		//更新
		int flag = bookDao.update(p);
		if(flag == 1) {
			//转发到主页
			request.getRequestDispatcher("/book?action=qryall").forward(request, response);
		}else {
			String msg = "更新失败！";
			request.getRequestDispatcher("/update.jsp").forward(request, response);
		}
		
	}
	
	protected void qryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// int -- Integer    double
		String id = request.getParameter("id");
		Book book = bookDao.qryById(Integer.parseInt(id));
		//
		request.setAttribute("book", book);
		//
		request.getRequestDispatcher("/update.jsp").forward(request, response);
	}
	
    protected void delById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String id = request.getParameter("id");
    	int flag = bookDao.delete(Integer.parseInt(id));
    	if(flag == 1) {
    		//转发到主页
			request.getRequestDispatcher("/book?action=qryall").forward(request, response);
    	}else {
    		request.getRequestDispatcher("/book?action=qryall").forward(request, response);
    	}
	}
	
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1 获取相关图书的数据
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String counts = request.getParameter("counts");
		String author = request.getParameter("author");
		String crtTime = request.getParameter("crtTime");
		
		Book p = new Book();
		p.setName(name);
		p.setPrice(Double.parseDouble(price));
		p.setCounts(Integer.parseInt(counts));
		p.setAuthor(author);
		p.setCrtTime(DateUtil.strToUtilDate(crtTime));
		
		int flag = bookDao.insert(p);
		if(flag == 1) {//添加成功
			//转发到主页
			request.getRequestDispatcher("/book?action=qryall").forward(request, response);
		}else {
			String msg = "添加失败";
			request.setAttribute("msg", msg);
			request.getRequestDispatcher("/add.jsp").forward(request, response);
		}
		
	}
	
	protected void qryall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Book> list = bookDao.qryAll();
		//把集合数据保存到request域对象中
		request.setAttribute("list", list);
		//转发到home页面
		 request.getRequestDispatcher("/home.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}


}
